home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / BuildingBlocks / ObjectList.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  5.1 KB  |  291 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ObjectList.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __OBJECTLIST__
  15. #include "ObjectList.h"
  16. #endif
  17.  
  18. #ifndef __MEMORY__
  19. #include <Memory.h>
  20. #endif
  21.  
  22. #ifndef    __DEBUGASSERT__
  23. #include "DebugAssert.h"
  24. #endif
  25.  
  26. #ifndef    __DEBUGGINGGEAR__
  27. #include "DebuggingGear.h"
  28. #endif
  29.  
  30. #pragma segment ObjectList
  31.  
  32. void* const kBadObject = (void*) 0xFFFFFFFF;
  33.  
  34. extern void BreakStr ( const Str255 str );
  35.  
  36. /***********************************|****************************************/
  37.  
  38. void
  39. TObjectList::SetSize ( unsigned long objects )
  40. {
  41.     if ( objects != fCount )
  42.     {
  43.         ::SetHandleSize ( (Handle) fList, objects * sizeof ( void* ) );
  44.         FAILOSErr ( MemError () );
  45.         fCount = objects;
  46.     }
  47. }
  48.  
  49. /***********************************|****************************************/
  50.  
  51. TObjectList::TObjectList ( Boolean ownsObjects ):
  52.     fList ( (void***) FAILNewHandle ( 0 ) ),
  53.     fCount ( 0 ),
  54.     fOwnsObjects ( ownsObjects )
  55. {
  56. }
  57.  
  58. /***********************************|****************************************/
  59.  
  60. TObjectList::~TObjectList ()
  61. {
  62.     if ( fCount > 0 && fOwnsObjects )
  63.         DeleteAll ();
  64.  
  65.     ::DeallocateHandle ( (Handle) fList );
  66. }
  67.  
  68. /***********************************|****************************************/
  69.  
  70. void
  71. TObjectList::SetOwnsObjects ( Boolean ownsObjects )
  72. {
  73.     fOwnsObjects = ownsObjects;
  74. }
  75.  
  76. /***********************************|****************************************/
  77.  
  78. Boolean
  79. TObjectList::GetOwnsObjects () const
  80. {
  81.     return fOwnsObjects;
  82. }
  83.  
  84. /***********************************|****************************************/
  85.  
  86. unsigned long
  87. TObjectList::Find ( const void* object ) const
  88. {
  89.     unsigned long index = fCount;
  90.  
  91.     while ( index-- > 0 )
  92.         if ( object == (*fList) [ index ] )
  93.             return index + 1;
  94.  
  95.     return 0;
  96. }
  97.  
  98. /***********************************|****************************************/
  99.  
  100. void*
  101. TObjectList::Get ( unsigned long index ) const
  102. {
  103.     if ( IsValidIndex ( index ) )
  104.     {
  105.         return (*fList) [ --index ];
  106.     }
  107.     else
  108.     {
  109. #if debug
  110.         BreakStr ( "\pinvalid index" );
  111. #endif
  112.         return nil;
  113.     }
  114. }
  115.  
  116. /***********************************|****************************************/
  117.  
  118. void*
  119. TObjectList::operator [] ( unsigned long index ) const
  120. {
  121.     if ( IsValidIndex ( index ) )
  122.     {
  123.         return (*fList) [ --index ];
  124.     }
  125.     else
  126.     {
  127. #if debug
  128.         BreakStr ( "\pinvalid index" );
  129. #endif
  130.         return nil;
  131.     }
  132. }
  133.  
  134. /***********************************|****************************************/
  135.  
  136. void
  137. TObjectList::Insert ( unsigned long index, void* object )
  138. {
  139. #if debug
  140.     if ( index == 0 )
  141.     {
  142.         BreakStr ( "\pzero user index" );
  143.         return;
  144.     }
  145. #endif
  146.  
  147.     if ( index >= fCount )
  148.         SetSize ( index );
  149.  
  150.     (*fList) [ --index ] = object;
  151. }
  152.  
  153. /***********************************|****************************************/
  154.  
  155. void*
  156. TObjectList::Remove ( unsigned long index )
  157. {
  158. #if debug
  159.     if ( !IsValidIndex ( index ) )
  160.     {
  161.         BreakStr ( "\pbad index" );
  162.         return nil;
  163.     }
  164. #endif
  165.  
  166.     void* object = (*fList) [ --index ];
  167.  
  168. #if debug
  169.     if ( object == nil )
  170.     {
  171.         BreakStr ( "\pnil object" );
  172.         return nil;
  173.     }
  174.  
  175.     if ( object == kBadObject )
  176.     {
  177.         BreakStr ( "\pbad object" );
  178.         return nil;
  179.     }
  180.  
  181.     (*fList) [ index ] = kBadObject;
  182. #endif
  183.  
  184.     unsigned long newCount = fCount - 1;
  185.  
  186.     if ( index < newCount )
  187.         ::BlockMove ( (*fList) + index + 1, (*fList) + index, ( newCount - index ) * sizeof ( void* ) );
  188.  
  189.     SetSize ( newCount );
  190.  
  191.     return object;
  192. }
  193.  
  194. /***********************************|****************************************/
  195.  
  196. Boolean
  197. TObjectList::Delete ( unsigned long index )
  198. {
  199.     void* object = Remove ( index );
  200.  
  201.     if ( object )
  202.     {
  203.         DeleteObject ( object );
  204.         return true;
  205.     }
  206.     else
  207.     {
  208.         return false;
  209.     }
  210. }
  211.  
  212. /***********************************|****************************************/
  213.  
  214. Boolean
  215. TObjectList::Remove ( const void* object )
  216. {
  217.     unsigned long index = Find ( object );
  218.  
  219.     if ( index > 0 )
  220.     {
  221.         Remove ( index );
  222.         return true;
  223.     }
  224.     else
  225.     {
  226.         return false;
  227.     }
  228. }
  229.  
  230. /***********************************|****************************************/
  231.  
  232. void
  233. TObjectList::RemoveAll ()
  234. {
  235.     SetSize ( 0 );
  236. }
  237.  
  238. /***********************************|****************************************/
  239.  
  240. Boolean
  241. TObjectList::Delete ( void* object )
  242. {
  243.     Boolean removed = Remove ( object );
  244.  
  245.     if ( removed )
  246.         DeleteObject ( object );
  247.  
  248.     return removed;
  249. }
  250.  
  251. /***********************************|****************************************/
  252.  
  253. void
  254. TObjectList::DeleteAll ()
  255. {
  256.     for ( unsigned long index = 0; index < fCount; index++ )
  257.         DeleteObject ( (*fList) [ index ] );
  258.  
  259.     SetSize ( 0 );
  260. }
  261.  
  262. /***********************************|****************************************/
  263.  
  264. void
  265. TObjectList::DeleteObject ( void* object ) const
  266. {
  267.     delete object;
  268. }
  269.  
  270. /***********************************|****************************************/
  271.  
  272. ostream&
  273. TObjectList::operator >> ( ostream& s ) const
  274. {
  275.     // subclasses should stream their name, then call this method
  276.  
  277.     s << " @ " << (void*) this << ": (" << Count() << ") ";
  278.  
  279.     for ( unsigned long index = 0; index < fCount; index++ )
  280.     {
  281.         if ( index > 0 )
  282.             s << ",";
  283.  
  284.         s << Get ( index + 1 );
  285.     }
  286.  
  287.     return s;
  288. }
  289.  
  290. /***********************************|****************************************/
  291.